package org.example.petsystem.controller;

import org.example.petsystem.entity.ResultBody;
import org.example.petsystem.entity.ShopMessage;
import org.example.petsystem.service.ShopManService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/ShopMessage")
@CrossOrigin(origins="*")
public class ShopManController {
    @Autowired
    private ShopManService shopManService;

    @RequestMapping("/UpdateShop")
     public ResultBody updateProduct(@RequestBody Map<String, Object> map) {
        ShopMessage pet = new ShopMessage();
        pet.setCode((String) map.get("code"));
        pet.setCategory((String) map.get("category"));
        pet.setName((String) map.get("name"));
        pet.setPrice(Double.valueOf(map.get("price").toString()));
        pet.setDescription((String) map.get("description"));
        pet.setPhone((String) map.get("phone"));
        pet.setAvatarUrl((String) map.get("avatar_Url"));
        pet.setGender((String) map.get("gender"));
        pet.setAge(map.get("age") == null ? null : Integer.valueOf(map.get("age").toString()));
        pet.setCertificate_url((String) map.get("certificate"));

        int result = shopManService.updatePet(pet);
        if (result > 0) {
            return new ResultBody(200, "更新成功");
        } else {
            return new ResultBody(400, "更新失败");
        }
    }

    @RequestMapping("/deleteShop")
    public ResultBody deletePet(@RequestBody Map<String, String> map) {
        String code = map.get("code");
        int result = shopManService.deletePet(code);
        if (result > 0) {
            return new ResultBody(200, "删除成功");
        } else {
            return new ResultBody(400, "删除失败");
        }
    }

}
